package com.caissa.framework.starter.autoconfigure;

import com.dianping.cat.Cat;
import com.dianping.cat.message.Message;
import com.dianping.cat.message.Transaction;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;

/**
 * Description:
 * User: forest
 * Date: 2021-01-12 11:25 上午
 */
public abstract class AspectBase{

    protected Object doCatInstrument(ProceedingJoinPoint pjp) throws Throwable {
        long start = System.currentTimeMillis();
        Transaction transaction = null;
        String metricName = getMetricName(pjp);
        try {
            transaction = Cat.newTransaction(getMetricType(), metricName);
            transaction.setStatus(Message.SUCCESS);
            return pjp.proceed();
        } catch (Throwable throwable){
            transaction.setStatus(throwable);
            throw throwable;
        }finally {
            transaction.complete();
            long end = System.currentTimeMillis();
            //System.out.println((end - start) + "ms," + metricName + "..." + pjp.getSignature());
        }
    }

    public String getMetricName(ProceedingJoinPoint pjp){
        MethodSignature joinPointObject = (MethodSignature) pjp.getSignature();
        return joinPointObject.getMethod().getName();
    }

    protected abstract String getMetricType();
}
